/*
 * BulletManager.cpp
 *
 *  Created on: 02/05/2013
 *      Author: Vitor
 */

#include "BulletManager.h"

std::vector<Bullet*> BulletManager::enemyBulletList;
std::vector<Bullet*> BulletManager::playerBulletList;

void BulletManager::instantiate(Point position, Vector2 velocity, bool isEnemy,
		Sprite* bulletSprite) {
	Bullet* newBullet;

	newBullet = new Bullet(position, velocity, isEnemy, bulletSprite);

	isEnemy ?
			enemyBulletList.push_back(newBullet) :
			playerBulletList.push_back(newBullet);

}

void BulletManager::update(double dt) {
	for (std::vector<Bullet*>::iterator it = enemyBulletList.begin();
			it != enemyBulletList.end(); it++) {
		(*it)->update(dt);
		if ((*it)->isDead()) {
			it = enemyBulletList.erase(it);
			it--;
		}
	}

	for (std::vector<Bullet*>::iterator it = playerBulletList.begin();
			it != playerBulletList.end(); it++) {
		(*it)->update(dt);
		if ((*it)->isDead()) {
			it = playerBulletList.erase(it);
			it--;
		}
	}
}

void BulletManager::render(float cameraX, float cameraY) {
	for (std::vector<Bullet*>::iterator it = playerBulletList.begin();
			it != playerBulletList.end(); it++) {
		(*it)->render(cameraX, cameraY);
	}
	for (std::vector<Bullet*>::iterator it = enemyBulletList.begin();
			it != enemyBulletList.end(); it++) {
		(*it)->render(cameraX, cameraY);
	}
}

Bullet* BulletManager::checkCollision(GameObject* target, bool enemy) {

	std::vector<Bullet*> vectorToCheck;

	enemy ? vectorToCheck = playerBulletList : vectorToCheck = enemyBulletList;

	for (std::vector<Bullet*>::iterator it = vectorToCheck.begin();
			it != vectorToCheck.end(); it++) {
		if (target->collidesWith(*it))
			return *it;
	}

	return NULL;
}

void BulletManager::cleanUp() {

	for (std::vector<Bullet*>::iterator it = enemyBulletList.begin();
			it != enemyBulletList.end(); it++) {
		it = enemyBulletList.erase(it);
		it--;
	}

	for (std::vector<Bullet*>::iterator it = playerBulletList.begin();
			it != playerBulletList.end(); it++) {
		it = playerBulletList.erase(it);
		it--;
	}

}
